package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.TableColumn;

import article.entities.ArticleVO;
import article.entities.CategoryVO;
import article.entities.IngredientVO;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

public class verProd {

	protected Shell shell;
	private Table table;
	ArticleVO [] arts;
	/**
	 * Launch the application.
	 * @param args
	 */
	public verProd(ArticleVO[] art, boolean variable) {
		//boolean = true es modificar
		try {
			arts=art;
			//verProd window = new verProd(arts);
			this.open(art, variable);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open( ArticleVO [] arts, boolean variable) {
		Display display = Display.getDefault();
		createContents(arts, variable);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents(ArticleVO[] arts, boolean variable) {
		shell = new Shell();
		shell.setSize(488, 338);
		shell.setText("Productos");
		shell.setLayout(new FormLayout());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(0, 193);
		fd_table.right = new FormAttachment(0, 460);
		fd_table.top = new FormAttachment(0, 57);
		fd_table.left = new FormAttachment(0, 10);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNombre = new TableColumn(table, SWT.NONE);
		tblclmnNombre.setWidth(100);
		tblclmnNombre.setText("Nombre");
		
		TableColumn tblclmnPrecio = new TableColumn(table, SWT.NONE);
		tblclmnPrecio.setWidth(100);
		tblclmnPrecio.setText("precio");
		
		TableColumn tblclmnIngrediente = new TableColumn(table, SWT.NONE);
		tblclmnIngrediente.setWidth(100);
		tblclmnIngrediente.setText("Ingrediente");
		
		TableColumn tblclmnDescripcion = new TableColumn(table, SWT.NONE);
		tblclmnDescripcion.setWidth(100);
		tblclmnDescripcion.setText("Descripcion");
		
		TableColumn tblclmnId = new TableColumn(table, SWT.NONE);
		tblclmnId.setWidth(100);
		tblclmnId.setText("id");
		
		Button btnModificar = new Button(shell, SWT.NONE);
		btnModificar.setVisible(variable);
		
			btnModificar.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					TableItem[] item=	table.getSelection();
					System.out.println(item.length);
					ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
					ArticleVO[] arts= null;
					try {
						arts = uso.getArticles();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					ArticleVO articulo = null;
					System.out.println("largo" + arts.length);
					for(int i=0;i<arts.length;i++){
						//System.out.println(Integer.parseInt(item[0].getText(4)));
						if(Integer.parseInt(item[0].getText(4))==(arts[i].getPrimaryKey())){
							articulo=arts[i];
							System.out.println("art "+ articulo.getPrimaryKey());
							VerProductoMod conf = new VerProductoMod (shell, shell.getStyle(), articulo);
							shell.setVisible(false);
							conf.open();
						}
						//System.out.println("art "+ articulo.getPrimaryKey());
					}
					
				//	Modificar conf = new Modificar(articulo);
					//conf.setVisible(true);
					}
				});
				
			FormData fd_btnModificar = new FormData();
			fd_btnModificar.top = new FormAttachment(table, 23);
			fd_btnModificar.left = new FormAttachment(0, 103);
			
			
			btnModificar.setLayoutData(fd_btnModificar);
			btnModificar.setText("Modifica");
			
			Button btnNewButton = new Button(shell, SWT.NONE);
			fd_btnModificar.right = new FormAttachment(100, -284);
			btnNewButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					TableItem[] item=	table.getSelection();
					System.out.println(item.length);
					ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
					ArticleVO[] arts= null;
					try {
						arts = uso.getArticles();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					ArticleVO articulo = null;
					System.out.println("largo" + arts.length);
					for(int i=0;i<arts.length;i++){
						//System.out.println(Integer.parseInt(item[0].getText(4)));
						if(Integer.parseInt(item[0].getText(4))==(arts[i].getPrimaryKey())){
							articulo=arts[i];
							EliminarProducto eliminar = new EliminarProducto(shell, shell.getStyle(), articulo);
							shell.setVisible(false);
							eliminar.open();
						}
					
					
				}}
			});
			FormData fd_btnNewButton = new FormData();
			fd_btnNewButton.right = new FormAttachment(btnModificar, 219, SWT.RIGHT);
			fd_btnNewButton.top = new FormAttachment(table, 23);
			fd_btnNewButton.left = new FormAttachment(btnModificar, 103);
			btnNewButton.setLayoutData(fd_btnNewButton);
			btnNewButton.setText("Eliminar");
			btnNewButton.setVisible(variable);
			
			Button btnVolver = new Button(shell, SWT.NONE);
			btnVolver.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					shell.close();
				}
			});
			FormData fd_btnVolver = new FormData();
			fd_btnVolver.bottom = new FormAttachment(100, -10);
			fd_btnVolver.right = new FormAttachment(100, -194);
			btnVolver.setLayoutData(fd_btnVolver);
			btnVolver.setText("Volver");
		
		//ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				//ArticleVO[] arts = uso.getArticles();
				
		
				for(int i=0;i<arts.length;i++){
					TableItem tableItem = new TableItem(table, SWT.NONE);
					//String [] x = new String[6];
					/*x[0] = arts[i].getName();
					x[1]= String.valueOf(arts[i].getPrice());
					x[2]= arts[i].getCat().getCat();
					x[3]= arts[i].getIngr()[0].getIngr();
					x[4]=arts[i].getDesc();
					x[5] = String.valueOf(arts[i].getPrimaryKey());
					*/
					tableItem.setText(0,  arts[i].getName());
					tableItem.setText(1, String.valueOf(arts[i].getPrice()));
					tableItem.setText(2, arts[i].getIngr()[0].getIngr());
					tableItem.setText(3,arts[i].getDesc());
					//tableItem.setText(4,arts[i].getDesc());
					tableItem.setText(4,String.valueOf(arts[i].getPrimaryKey()));
					
					
					//tableItem.setText(x);
					
					
				}
				
		    
	}

}
